package com.jz.community.moduleauthorization.login.model;

import android.app.Activity;
import android.content.Context;

import com.jz.community.basecomm.bean.BaseResponseInfo;
import com.jz.community.basecomm.bean.UserInfo;
import com.jz.community.basecomm.net.retrofit.ApiException;
import com.jz.community.basecomm.net.retrofit.ApiUtils;
import com.jz.community.basecomm.net.retrofit.HttpRxObservable;
import com.jz.community.basecomm.net.retrofit.RxLoadingWrapper;
import com.jz.community.basecomm.net.rxandroid.OnLoadListener;
import com.jz.community.basecomm.bean.LoginInfo;
import com.jz.community.moduleauthorization.login.net.LoginResetPwdApi;
import com.jz.community.moduleauthorization.login.task.GetLoginPwdTask;
import com.jz.community.basecomm.task.GetSmsCodeTask;
import com.trello.rxlifecycle2.components.support.RxFragmentActivity;

import java.util.HashMap;

import io.reactivex.Observable;
import io.reactivex.functions.Consumer;

/**
 * Created by Tony.Fan on 2018/3/20 13:37
 */
public class LoginModelImpl implements LoginModel {

    private Context mContext;

    public LoginModelImpl(Context mContext) {
        this.mContext = mContext;
    }

    @Override
    public void requestLoginInfo(String mobi, String password, String loginType, String params, String deviceId, String registrationId, final OnLoadListener<LoginInfo> onLoadListener) {

        new GetLoginPwdTask((Activity) mContext, o -> {
            if (o == null) {
                return;
            }
            onLoadListener.onSuccess((LoginInfo) o);
        }).execute(mobi, password, loginType, params, deviceId, registrationId);
    }

    @Override
    public void requestGetSmsCode(String type, String mobi, OnLoadListener<LoginInfo> onLoadListener) {
        new GetSmsCodeTask((Activity) mContext,false, o -> {
            if (o == null) {
                return;
            }
            onLoadListener.onSuccess((LoginInfo) o);
        }).execute(type, mobi);
    }

    @Override
    public void requestGetResetPwd(HashMap<String, Object> map, OnLoadListener<BaseResponseInfo> onLoadListener) {

        Observable<UserInfo> infoObservable = HttpRxObservable.getObservable((RxFragmentActivity) mContext, ApiUtils.getApi(mContext, LoginResetPwdApi.class)
                .getResetInfo(map.get("username").toString(), map.get("password").toString()));

        new RxLoadingWrapper<UserInfo>(mContext)
                .execute(infoObservable)
                .subscribe(new Consumer<BaseResponseInfo>() {
                    @Override
                    public void accept(BaseResponseInfo userInfo) throws Exception {
                        onLoadListener.onSuccess(userInfo);
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        onLoadListener.onFail(ApiException.handleException(throwable).message, ApiException.handleException(throwable).code);
                    }
                });
    }

    @Override
    public void requestBindJupsh(HashMap<String, Object> map) {
        Observable<String> infoObservable = HttpRxObservable.getObservable((RxFragmentActivity) mContext, ApiUtils.getApi(mContext, LoginResetPwdApi.class)
                .getBindJupsh(map));
        new RxLoadingWrapper<String>(mContext)
                .execute(infoObservable)
                .subscribe(new Consumer<String>() {
                    @Override
                    public void accept(String userInfo) throws Exception {
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                    }
                });
    }
}
